#include "lvgl/lvgl.h"
#include <pthread.h>
#include <unistd.h>
#include <string.h>

// 状态定义
#define STATE_HOME 0
#define STATE_LOGIN 1
#define STATE_REGISTER 2
volatile int ui_state = STATE_HOME;

// 输入缓存
char username_input[50] = {0};
char password_input[50] = {0};

// LVGL对象
lv_obj_t* login_btn;
lv_obj_t* register_btn;
lv_obj_t* username_ta;
lv_obj_t* password_ta;
lv_obj_t* keyboard;
lv_obj_t* msg_label;

// 创建主界面
void create_home_screen(lv_obj_t* parent) {
    lv_obj_clean(parent);
    
    // 标题
    lv_obj_t* title = lv_label_create(parent);
    lv_label_set_text(title, "智慧小区登录系统");
    lv_obj_set_style_text_font(title, &lv_font_montserrat_32, 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 50);
    
    // 注册按钮
    register_btn = lv_btn_create(parent);
    lv_obj_set_size(register_btn, 200, 50);
    lv_obj_align(register_btn, LV_ALIGN_CENTER, 0, -50);
    lv_obj_t* reg_label = lv_label_create(register_btn);
    lv_label_set_text(reg_label, "注册");
    
    // 登录按钮
    login_btn = lv_btn_create(parent);
    lv_obj_set_size(login_btn, 200, 50);
    lv_obj_align(login_btn, LV_ALIGN_CENTER, 0, 50);
    lv_obj_t* login_label = lv_label_create(login_btn);
    lv_label_set_text(login_label, "登录");
}

// 创建输入界面
void create_input_screen(lv_obj_t* parent) {
    lv_obj_clean(parent);
    
    // 返回按钮
    lv_obj_t* back_btn = lv_btn_create(parent);
    lv_obj_set_size(back_btn, 100, 40);
    lv_obj_align(back_btn, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_obj_t* back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    
    // 用户名输入框
    lv_obj_t* user_label = lv_label_create(parent);
    lv_label_set_text(user_label, "用户名:");
    lv_obj_align(user_label, LV_ALIGN_TOP_LEFT, 50, 100);
    username_ta = lv_textarea_create(parent);
    lv_textarea_set_placeholder_text(username_ta, "输入用户名");
    lv_obj_set_size(username_ta, 350, 40);
    lv_obj_align_to(username_ta, user_label, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
    
    // 密码输入框
    lv_obj_t* pwd_label = lv_label_create(parent);
    lv_label_set_text(pwd_label, "密码:");
    lv_obj_align(pwd_label, LV_ALIGN_TOP_LEFT, 50, 170);
    password_ta = lv_textarea_create(parent);
    lv_textarea_set_placeholder_text(password_ta, "输入密码");
    lv_textarea_set_password_mode(password_ta, true);
    lv_obj_set_size(password_ta, 350, 40);
    lv_obj_align_to(password_ta, pwd_label, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
    
    // 虚拟键盘
    keyboard = lv_keyboard_create(parent);
    lv_obj_set_size(keyboard, 600, 200);
    lv_obj_align(keyboard, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_keyboard_set_textarea(keyboard, username_ta);
    
    // 消息标签
    msg_label = lv_label_create(parent);
    lv_obj_set_width(msg_label, 400);
    lv_obj_align(msg_label, LV_ALIGN_TOP_MID, 0, 20);
}

// 按钮事件回调
static void btn_event_cb(lv_event_t* e) {
    lv_obj_t* target = lv_event_get_target(e);
    
    if (target == login_btn) {
        ui_state = STATE_LOGIN;
        create_input_screen(lv_scr_act());
    } 
    else if (target == register_btn) {
        ui_state = STATE_REGISTER;
        create_input_screen(lv_scr_act());
    }
}
